--    Program: 		CAPPOEXT_APPROVAL_GROUP_ASSIGNMENT.sql
--    Description:	Extract PO Approval Group Assignment information 
--    Revision:		1st     
--    Parameter: 	1. set of books short name e.g. SOB_C999

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;
alter session set nls_date_format='YYYYMMDD HH24:MI:SS';

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
                    		
   l_utl_file       		utl_file.file_type;
   l_filename       		varchar2(255);
   l_pathname       		varchar2(100);
                    		
   l_ngo_sob				varchar2(20);
   
   l_ngo_sob_id		        number;
   
   cursor cur_assig (p_sob_id number) is
      select ppc.* 
      from   PO_POSITION_CONTROLS_ALL ppc,
	         hr_organization_information hoi
      where  hoi.org_information_context = 'Operating Unit Information'
      and    hoi.org_information3 = to_char(p_sob_id)
      and    hoi.organization_id = ppc.org_id;

begin
   l_pathname := '/tmp';
   l_filename := 'CAPPO_APPROVAL_GROUP_ASSIGNMENT.dat';
   l_utl_file := utl_file.fopen(l_pathname, l_filename, 'w');
   
   l_processed_count := 0;
   l_rejected_count  := 0;
   
   l_ngo_sob := '&1';
   
    begin
      select set_of_books_id
      into l_ngo_sob_id
      from gl_sets_of_books 
      where upper(short_name) = upper(l_ngo_sob);
    exception
      when others then
        dbms_output.put_line(l_ngo_sob||': set of books name not found');
    end;

   for cur in cur_assig(l_ngo_sob_id) loop   	
     
     -- output data
     utl_file.put_line( l_utl_file, 
   	 cur.position_id|| chr(9) ||           
     cur.control_group_id|| chr(9) ||       
     cur.last_update_date|| chr(9) ||       
     cur.last_updated_by|| chr(9) ||         
     cur.last_update_login|| chr(9) ||      
     cur.creation_date|| chr(9) ||           
     cur.created_by|| chr(9) ||             
     cur.job_id|| chr(9) ||                  
     cur.organization_id|| chr(9) ||         
     cur.start_date|| chr(9) ||            
     cur.end_date|| chr(9) ||                
     cur.control_function_id|| chr(9) ||     
     cur.program_application_id|| chr(9) ||  
     cur.program_id|| chr(9) ||              
     cur.program_update_date|| chr(9) ||    
     cur.request_id|| chr(9) ||           
     cur.attribute1|| chr(9) ||        
     cur.attribute10|| chr(9) ||       
     cur.attribute11|| chr(9) ||        
     cur.attribute12|| chr(9) ||          
     cur.attribute13|| chr(9) ||          
     cur.attribute14|| chr(9) ||          
     cur.attribute15|| chr(9) ||            
     cur.attribute2|| chr(9) ||             
     cur.attribute3|| chr(9) ||            
     cur.attribute4|| chr(9) ||            
     cur.attribute5|| chr(9) ||           
     cur.attribute6|| chr(9) ||         
     cur.attribute7|| chr(9) ||        
     cur.attribute8|| chr(9) ||            
     cur.attribute9|| chr(9) ||            
     cur.attribute_category|| chr(9) ||     
     to_char(cur.org_id)
	 );
     
	 l_processed_count := l_processed_count + 1;
   end loop;

   if l_processed_count = 0 then   
     l_rejected_count := l_rejected_count + 1;
     l_err_msg := l_ngo_sob || ': Approval Group Assignment not found';
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPPOEXT_APPROVAL_GROUP_ASSIGNMENT', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;

   utl_file.fclose(l_utl_file);
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/

--exit
